package edu.westga.cs3152.wordmojo;

import java.util.ArrayList;

/**
 * Parses the words played on the board.
 * 
 * @author Lewis Williams
 * @version 2011.5.3
 *
 */
public class BoardParser {
	
	private GameBoard board;
	private ArrayList<String> attemptedWords = new ArrayList<String>();
	
	public BoardParser(GameBoard board){
		this.board = board;
	}

	public ArrayList<String> getAttemptedWords() {
		parseBoard();
		return attemptedWords;
	}
	
	/**
	 * Parses the board vertically and horizontally, 
	 * retrieving the words the player attempts to play.
	 */
	private void parseBoard(){
		testSingleLetters();
		String[] rowStrings = {"","","","","","","","",""};
		String[] columnStrings = {"","","","","","","","",""};
		
		for(int row = 0; row < board.getBOARD_ROWS(); row++){   		
    		for(int column = 0; column < board.getBOARD_COLUMNS(); column++){
    			Letter currentLetter = board.getPiece(row, column);
    			rowStrings[row] += currentLetter.getLetter();
    			columnStrings[column] += currentLetter.getLetter();
    		}
		}

		//Extracts words played horizontally
		for(int i = 0; i < 9; i++){
			String words = rowStrings[i];
			String currentWord = "";
			for(int j = 0; j < 9; j++){
				if(words.charAt(j) != '@'){
					currentWord += words.charAt(j);
					if(j == 8 && currentWord.length() > 2){
						attemptedWords.add(currentWord);
					}
				}
				else if(words.charAt(j) == '@'){
					if(currentWord != "@" && currentWord != "" && currentWord.length() > 1){
						attemptedWords.add(currentWord);
					}
					currentWord = "";
				}
			}
		}
		
		//Extracts words played vertically
		for(int i = 0; i < 9; i++){
			String words = columnStrings[i];
			String currentWord = "";
			for(int j = 0; j < 9; j++){
				if(words.charAt(j) != '@'){
					currentWord += words.charAt(j);
					if(j == 8 && currentWord.length() > 2){
						attemptedWords.add(currentWord);
					}
				}
				else if(words.charAt(j) == '@'){
					if(currentWord != "@" && currentWord != "" && currentWord.length() > 1){
						attemptedWords.add(currentWord);
					}
					currentWord = "";
				}
			}
		}
		
		findTheNumberOfWordsPlayed();
	}
	
	/**
	 * Discovers single letters by themselves and makes sure they don't end up on the board.
	 */
	private void testSingleLetters(){
		for(int row = 0; row < board.getBOARD_ROWS(); row++){   		
    		for(int column = 0; column < board.getBOARD_COLUMNS(); column++){
    			String neighbors = "";
    			if(board.getPiece(row, column).getLetter() != '@'){
    				neighbors += board.getPiece(row, column - 1).getLetter();
    				neighbors += board.getPiece(row, column + 1).getLetter();
    				neighbors += board.getPiece(row - 1, column).getLetter();
    				neighbors += board.getPiece(row + 1, column - 1).getLetter();
    			}
    			if(neighbors.equalsIgnoreCase("@@@@")){
    				board.setSingleLetterError(true);
    			}
    		}
		}
	}
		
	/**
	 * Returns the number of words played on the board.
	 */
	private void findTheNumberOfWordsPlayed(){
		board.setNumberOfWordsOnBoard(attemptedWords.size());
	}
	
}
